/**
 * 
 */
package edu.umd.clip.lm.factors;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class PlainInputParser extends InputParser {

	/**
	 * @param desc
	 */
	public PlainInputParser(FactorTupleDescription desc) {
		super(desc);
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.factors.InputParser#tupleToWord(long)
	 */
	@Override
	public String tupleToWord(long tuple) {
		byte idx = tupleDescription.getMainFactorIndex();
		int word = FactorTuple.getValue(tuple, idx);
		return tupleDescription.getDictionary(idx).getWord(word);
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.factors.InputParser#tupleWordToSurfaceWord(java.lang.String)
	 */
	@Override
	public String tupleWordToSurfaceWord(String word) {
		return word;
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.factors.InputParser#wordToTuple(java.lang.String)
	 */
	@Override
	public long wordToTuple(String word) {
		if (Dictionary.isStartWord(word)) {
			return tupleDescription.createStartTuple() & tupleDescription.getOvertFactorsMask();
		}
		if (Dictionary.isEndWord(word)) {
			return tupleDescription.createEndTuple() & tupleDescription.getOvertFactorsMask();
		}
		
		long tuple = 0;
		for(byte idx : tupleDescription.getOvertFactors()) {
			FactorDescription desc = tupleDescription.getDescription(idx);
			if (idx == tupleDescription.getMainFactorIndex()) {
				tuple = FactorTuple.setValue(tuple, idx, desc.getDictionary().getId(word));
			} else {
				String overtFactor = desc.getWordToOvertFactor().wordToOvertFactor(word);
				tuple = FactorTuple.setValue(tuple, idx, desc.getDictionary().getId(overtFactor));
			}
		}
		return tuple;
	}

}
